package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.player.NewAddPlayerManager;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 玩家分析-新增分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/player/newPlayer")
public class NewPlayerBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;

	// 新增玩家数据列表
	private List<NewAddPlayerManager.NewPlayerData> newPlayerDatas;
    // 分渠道新增玩家数据列表
    private List<NewAddPlayerManager.NewPlayerByChannelData> newPlayerByChannelDatas;
    // 设备激活柱状图
    private ChartUIData deviceActiveChart;
    // 设备激活-新增转化率线状图
    private ChartUIData deviceActive2NewChart;
    // 帐号新增线状图
    private ChartUIData userNewChart;
    // 帐号分渠道新增线状图
    private ChartUIData userNewByChannelChart;
	
	@Autowired
	private NewAddPlayerManager newAddPlayerManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            newPlayerDatas = newAddPlayerManager.getNewPlayerDatas(dateStart, dateEnd, platform, channel, server);
            if (newPlayerDatas != null && newPlayerDatas.size() > 0) {
                deviceActiveChart = new ChartUIData();
                deviceActiveChart.setJsonType(ChartType.COLUMN.type);
                deviceActiveChart.setJsonStr(new ChartTool(newPlayerDatas, NewAddPlayerManager.NewPlayerData.class, "activeDevice").loadData(deviceActiveChart.getJsonType()));

                userNewChart = new ChartUIData();
                userNewChart.setJsonType(ChartType.SPLINE.type);
                userNewChart.setJsonStr(new ChartTool(newPlayerDatas, NewAddPlayerManager.NewPlayerData.class, "newUser").loadData(userNewChart.getJsonType()));

                deviceActive2NewChart = new ChartUIData();
                deviceActive2NewChart.setJsonType(ChartType.SPLINE.type);
                deviceActive2NewChart.setJsonStr(new ChartTool(newPlayerDatas, NewAddPlayerManager.NewPlayerData.class, "active2NewRadio").loadData(deviceActive2NewChart.getJsonType()));
            } else {
                deviceActiveChart = null;
                userNewChart = null;
                deviceActive2NewChart = null;
            }

            newPlayerByChannelDatas = newAddPlayerManager.getNewPlayerByChannelDatas(dateStart, dateEnd, platform, channel, server);
            if (newPlayerByChannelDatas != null && newPlayerByChannelDatas.size() > 0) {
                userNewByChannelChart = new ChartUIData();
                userNewByChannelChart.setJsonType(ChartType.SPLINE.type);
                userNewByChannelChart.setJsonStr(new ChartTool(newPlayerByChannelDatas, NewAddPlayerManager.NewPlayerByChannelData.class, "").loadData(userNewByChannelChart.getJsonType()));
            } else {
                userNewByChannelChart = null;
            }

            result.put("res", "0");
            result.put("newPlayerDatas", newPlayerDatas);
            result.put("newPlayerByChannelDatas", newPlayerByChannelDatas);
            result.put("deviceActiveChart", deviceActiveChart);
            result.put("deviceActive2NewChart", deviceActive2NewChart);
            result.put("userNewChart", userNewChart);
            result.put("userNewByChannelChart", userNewByChannelChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {
    }

    @Override
	protected String csvContent() {
		return "新增分析";
	}

	@Override
    protected String csvFileName() {
	    return "新增分析";
    }

    public List<NewAddPlayerManager.NewPlayerData> getNewPlayerDatas() {
        return newPlayerDatas;
    }

    public void setNewPlayerDatas(List<NewAddPlayerManager.NewPlayerData> newPlayerDatas) {
        this.newPlayerDatas = newPlayerDatas;
    }

    public List<NewAddPlayerManager.NewPlayerByChannelData> getNewPlayerByChannelDatas() {
        return newPlayerByChannelDatas;
    }

    public void setNewPlayerByChannelDatas(List<NewAddPlayerManager.NewPlayerByChannelData> newPlayerByChannelDatas) {
        this.newPlayerByChannelDatas = newPlayerByChannelDatas;
    }

    public ChartUIData getDeviceActiveChart() {
        return deviceActiveChart;
    }

    public void setDeviceActiveChart(ChartUIData deviceActiveChart) {
        this.deviceActiveChart = deviceActiveChart;
    }

    public ChartUIData getDeviceActive2NewChart() {
        return deviceActive2NewChart;
    }

    public void setDeviceActive2NewChart(ChartUIData deviceActive2NewChart) {
        this.deviceActive2NewChart = deviceActive2NewChart;
    }

    public ChartUIData getUserNewChart() {
        return userNewChart;
    }

    public void setUserNewChart(ChartUIData userNewChart) {
        this.userNewChart = userNewChart;
    }

    public ChartUIData getUserNewByChannelChart() {
        return userNewByChannelChart;
    }

    public void setUserNewByChannelChart(ChartUIData userNewByChannelChart) {
        this.userNewByChannelChart = userNewByChannelChart;
    }
}
